<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||	
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws. 
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.). 
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations). 
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Account/classes/PhpFox_ServiceInvite.class.php, $Author:  $ */

/** Service for working with invite friends functionality
 * 
 * @package module.account
 * @version $Id: PhpFox_ServiceInvite.class.php 46 2007-12-14 23:12:21Z phpfoxsv $
 *   
 */
class PhpFox_Mod_Account_ServiceInvite extends BaseService 
{
	/** Send invitation emails
	 * @param array                          $aEmails  emails array
	 * @param PhpFox_Mod_Account_ItemAccount $oAcc     sender user account object
	 * @return array sended emails
	 */
	function sendInvites($aEmails, $oAcc, $sText)
	{
	    $oTpl = &App::getTemplate();
        //send email
        $oMailer = &App::newObject('mail_Mailer');
		$aVars = array(
			'SITENAME'    => App::getSetting('site_name'),
			'USERNAME'    => $oAcc->aData['user'],
		    'HOME'        => App::makeUrl('public.signup', array('refid'=>$oAcc->aData[$oAcc->sId])),
			'EXTRATEXT'   => $sText,
		);
		$oInvite = $this->_oModule->getItem('Invite');
		/* @var $oInvite PhpFox_Mod_Account_ItemInvite */
		
		foreach($aEmails as $sEmail)
		{
			$oMailer->send('email_invite', $sEmail, ''. App::format('core.email.invite') .' '.$oAcc->aData['user'], $aVars);
	    	$oInvite->aData['user']  = $oAcc->aData['user'];
	    	$oInvite->aData['email'] = $sEmail;
	    	unset($oInvite->aData[$oInvite->sId]);
	    	$oInvite->insert();
		}
		if ($aEmails)
		{
		    $oSrvAcc = $this->_oModule->getService('Account');
		    /* @var $oSrvAcc PhpFox_Mod_Account_ServiceAccount */
		    $oSrvAcc->changeUserInfo($oAcc, 'invite', count($aEmails));
		}
		return $aEmails;
	}
	
	function isInvitedByUser($sEmail)
	{
		$aCheck = $this->oDatabase->getRow("SELECT id FROM ". App::getT('invite') ." WHERE email = '". $sEmail ."'");
		if ( isset($aCheck['id']) )
		{
			$this->oDatabase->update(App::getT('invite'), array('type' => '1'), " id = '". $aCheck['id'] ."' ");
			return true;
		}
		
		return false;
	}
	
	function getInvites($sUser)
	{
		$aInvites = $this->oDatabase->getRows("SELECT id, email FROM ". App::getT('invite') ." WHERE user = '". $sUser ."' AND type != '1'");
		return $aInvites;
	}
	
	function deleteInvite($iId)
	{
		$this->oDatabase->_delete(App::getT('invite'), " id = '". $iId ."' LIMIT 1 ");
		return true;
	}

	/** Get invitations list
	 *
	 * @param array  $aCond      select condition
	 * @param int    $iPage      page number
	 * @param int    $iPageSize  page size
	 * @param string $sOrder     order string
	 * @return array invitations list
	 */
	function &getList($aCond = array(), $iPage=0, $iPageSize=0, $sOrder = '')
	{
	    $oInvite = $this->_oModule->getItem('Invite');
        /* @var $oInvite Mod_Account_Invite */

        $aRes = $oInvite->getList($aCond, $iPage, $iPageSize, $sOrder);
        return $aRes;
	}

}
?>